const contentTypeCheck = {
  async checkJsonContentType (ctx, next) {
    const contentType = ctx.request.headers['content-type'];
    // 只允许 application/json 格式的请求
    if (contentType && contentType.includes('application/json')) {
      await next();  // 如果是 application/json，继续执行后续中间件
    } else {
      ctx.status = 415;  // 415 Unsupported Media Type
      ctx.body = {
        success: false,
        msg: 'Content-Type must be application/json'
      };
    }
  },
  async checkUrlencodedContentType (ctx, next) {
    const contentType = ctx.request.headers['content-type'];
    // 只允许 x-www-form-urlencoded 格式的请求
    if (contentType && contentType.includes('x-www-form-urlencoded')) {
      await next();  // 如果是 x-www-form-urlencoded，继续执行后续中间件
    } else {
      ctx.status = 415;  // 415 Unsupported Media Type
      ctx.body = {
        success: false,
        msg: 'Content-Type must be x-www-form-urlencoded'
      };
    }
  }
}

module.exports = contentTypeCheck